Conditions | 1 |
Paths | 92 |
Total Lines | 120 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(['helper'], function (helper) { |
||
2 | var self = {}; |
||
3 | |||
4 | var ctx; |
||
5 | var width; |
||
6 | var height; |
||
7 | var transform; |
||
8 | var highlight; |
||
9 | |||
10 | var NODE_RADIUS = 15; |
||
11 | var LINE_RADIUS = 12; |
||
12 | |||
13 | function drawDetailNode(d) { |
||
14 | if (transform.k > 1 && d.o.is_online) { |
||
15 | helper.positionClients(ctx, d, Math.PI, d.o, 15); |
||
16 | ctx.beginPath(); |
||
17 | var name = d.o.node_id; |
||
18 | if (d.o) { |
||
19 | name = d.o.hostname; |
||
20 | } |
||
21 | ctx.textAlign = 'center'; |
||
22 | ctx.fillStyle = config.forceGraph.labelColor; |
||
23 | ctx.fillText(name, d.x, d.y + 20); |
||
24 | } |
||
25 | } |
||
26 | |||
27 | function drawHighlightNode(d) { |
||
28 | if (highlight && highlight.type === 'node' && d.o.node_id === highlight.id) { |
||
29 | ctx.arc(d.x, d.y, NODE_RADIUS * 1.5, 0, 2 * Math.PI); |
||
30 | ctx.fillStyle = config.forceGraph.highlightColor; |
||
31 | ctx.fill(); |
||
32 | ctx.beginPath(); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | function drawHighlightLink(d, to) { |
||
37 | if (highlight && highlight.type === 'link' && d.o.id === highlight.id) { |
||
38 | ctx.lineTo(to[0], to[1]); |
||
39 | ctx.strokeStyle = config.forceGraph.highlightColor; |
||
40 | ctx.lineWidth = LINE_RADIUS * 2; |
||
41 | ctx.lineCap = 'round'; |
||
42 | ctx.stroke(); |
||
43 | to = [d.source.x, d.source.y]; |
||
44 | } |
||
45 | return to; |
||
46 | } |
||
47 | |||
48 | self.drawNode = function drawNode(d) { |
||
49 | if (d.x < transform.invertX(0) || d.y < transform.invertY(0) || transform.invertX(width) < d.x || transform.invertY(height) < d.y) { |
||
50 | return; |
||
51 | } |
||
52 | ctx.beginPath(); |
||
53 | |||
54 | drawHighlightNode(d); |
||
55 | |||
56 | if (d.o.is_online) { |
||
57 | ctx.arc(d.x, d.y, 8, 0, 2 * Math.PI); |
||
58 | if (d.o.is_gateway) { |
||
59 | ctx.rect(d.x - 9, d.y - 9, 18, 18); |
||
60 | } |
||
61 | ctx.fillStyle = config.forceGraph.nodeColor; |
||
62 | } else { |
||
63 | ctx.arc(d.x, d.y, 6, 0, 2 * Math.PI); |
||
64 | ctx.fillStyle = config.forceGraph.nodeOfflineColor; |
||
65 | } |
||
66 | |||
67 | ctx.fill(); |
||
68 | |||
69 | drawDetailNode(d); |
||
70 | }; |
||
71 | |||
72 | self.drawLink = function drawLink(d) { |
||
73 | var zero = transform.invert([0, 0]); |
||
74 | var area = transform.invert([width, height]); |
||
75 | if (d.source.x < zero[0] && d.target.x < zero[0] || d.source.y < zero[1] && d.target.y < zero[1] || |
||
76 | d.source.x > area[0] && d.target.x > area[0] || d.source.y > area[1] && d.target.y > area[1]) { |
||
77 | return; |
||
78 | } |
||
79 | ctx.beginPath(); |
||
80 | ctx.moveTo(d.source.x, d.source.y); |
||
81 | var to = [d.target.x, d.target.y]; |
||
82 | |||
83 | to = drawHighlightLink(d, to); |
||
84 | |||
85 | var grd = ctx.createLinearGradient(d.source.x, d.source.y, d.target.x, d.target.y); |
||
86 | grd.addColorStop(0.45, d.color); |
||
87 | grd.addColorStop(0.55, d.color_to); |
||
88 | |||
89 | ctx.lineTo(to[0], to[1]); |
||
90 | ctx.strokeStyle = grd; |
||
91 | if (d.o.type.indexOf('vpn') === 0) { |
||
92 | ctx.globalAlpha = 0.2; |
||
93 | ctx.lineWidth = 1.5; |
||
94 | } else { |
||
95 | ctx.globalAlpha = 0.8; |
||
96 | ctx.lineWidth = 2.5; |
||
97 | } |
||
98 | ctx.stroke(); |
||
99 | ctx.globalAlpha = 1; |
||
100 | }; |
||
101 | |||
102 | self.setCTX = function setCTX(newValue) { |
||
103 | ctx = newValue; |
||
104 | }; |
||
105 | |||
106 | self.setHighlight = function setHighlight(newValue) { |
||
107 | highlight = newValue; |
||
108 | }; |
||
109 | |||
110 | self.setTransform = function setTransform(newValue) { |
||
111 | transform = newValue; |
||
112 | }; |
||
113 | |||
114 | self.setMaxArea = function setMaxArea(newWidth, newHeight) { |
||
115 | width = newWidth; |
||
116 | height = newHeight; |
||
117 | }; |
||
118 | |||
119 | return self; |
||
120 | }); |
||
121 |